package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.ClazzMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageResult;
import com.itheima.service.ClazzService;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ClazzServiceImpl extends ServiceImpl<ClazzMapper, Clazz> implements ClazzService {
    @Autowired
    private EmpService empService;

    /**
     * 分页条件查询班级
     *
     * @param name
     * @param begin
     * @param end
     * @param page
     * @param pageSize
     * @return PageResult<Clazz>
     */
    @Override
    public PageResult<Clazz> getClassByPage(String name, LocalDate begin, LocalDate end, int page, int pageSize) {
        Page<Clazz> pageClazz = Page.of(page, pageSize);
        LambdaQueryWrapper<Clazz> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(begin != null, Clazz::getBeginDate, begin)
                .eq(end != null, Clazz::getEndDate, end)
                .like(name != null && !name.isEmpty(), Clazz::getName, name);
        pageClazz = page(pageClazz, queryWrapper);
        List<Clazz> clazzList = pageClazz.getRecords();
        List<Integer> masterIds = clazzList.stream().map(clazz -> clazz.getMasterId()).collect(Collectors.toList());
        if (masterIds != null && !masterIds.isEmpty()) {
            List<Emp> empList = empService.listByIds(masterIds);
            clazzList.forEach(clazz -> {
                empList.forEach(emp -> {
                    if (clazz.getMasterId() != null && clazz.getMasterId() == emp.getId()) {
                        clazz.setMasterName(emp.getName());
                    }
                });
            });
        }
        LocalDate now = LocalDate.now();
        clazzList = clazzList.stream().map(clazz -> {
            if (clazz.getBeginDate().isAfter(now)) {
                clazz.setStatus("未开班");
            } else if (clazz.getEndDate().isBefore(now)) {
                clazz.setStatus("已结课");
            } else {
                clazz.setStatus("开课中");
            }
            return clazz;
        }).collect(Collectors.toList());
        PageResult<Clazz> pageResult = new PageResult<>(pageClazz.getTotal(), clazzList);
        return pageResult;
    }

    /**
     * 新增班级
     *
     * @param clazz
     */
    @Override
    public void saveClazz(Clazz clazz) {
        String masterName = clazz.getMasterName();
        LambdaQueryWrapper<Emp> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(masterName != null && !masterName.isEmpty(), Emp::getName, masterName);
        List<Emp> list = empService.list(queryWrapper);
        Integer masterId = 0;
        if (list != null && !list.isEmpty()) {
            masterId = list.get(0).getId();
        }
        clazz.setMasterId(masterId);
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        save(clazz);
    }

    /**
     * 根据ID查询班级
     *
     * @param id
     * @return
     */
    @Override
    public Clazz getClazzById(Integer id) {
        Clazz clazz = getById(id);
        Integer masterId = clazz.getMasterId();
        Emp emp = empService.getEmpById(masterId);
        String masterName = emp.getName();
        clazz.setMasterName(masterName);
        return clazz;
    }
}
